<?php

namespace App\Http\Controllers\Admin;

use App\Model\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PhpParser\Node\Expr\Array_;

class TagController extends Controller
{
    //

    private $tagModel = null;


    public function __construct()
    {
        $this->tagModel = new Tag();

    }


    public function addTag(Request $request)
    {  //添加标签

        $name = $request->input('name');

        if (!$name) {
            return response()->json(array('code' => 0, 'msg' => '缺少分类名称'));
        }

        $result = $this->tagModel->create($request->input());

        if ($result) {
            return response()->json(array('code' => 1, 'msg' => '添加标签成功'));
        } else {
            return response()->json(array('code' => 0, 'msg' => '添加标签失败'));
        }
    }

    public function get_first_level()
    {  //获取一级标签
        $result = $this->tagModel::where('enabled', 1)->get();
        return response()->json(['code' => 1, 'msg' => '获取成功', 'data' => $result]);
    }

    public function get_all_list()
    {  //获取所有标签 并且分类下级
//        $tagAll = $this->tagModel::all()->where('enabled',1);
//        $one_level = $this->tagModel::where('parent_id',0)->where('enabled',1)->get();
//        foreach ($one_level as $kl=>$vl){
//            $id = $vl['id'];
//            $ret = [];
//            foreach ($tagAll as $k=>$v){
//                if($id == $v['parent_id']){
//                    array_push($ret,$v);
//                    unset($k);
//                }
//            }
//            $vl['two_level'] = $ret;
//        }
//        if($one_level){
//            return response()->json(['code'=>1,'msg'=>'获取成功','data'=>$one_level]);
//        }else{
//            return response()->json(['code'=>0,'msg'=>'获取失败','data'=>$one_level]);
//        }

        $ret = $this->getTagTree();

        return response()->json(['code' => 1, 'msg' => '获取成功', 'data' => $ret]);


    }

    public function getTagTree($pid = 0)
    {
        $rows = $this->tagModel::where('parent_id', '=', $pid)->where('enabled',1)->get();
        $tmp = array();
        if (sizeof($rows !== 0)) {
            foreach ($rows as $k => $v) {
                $v['children'] = $this->getTagTree($v['id']);
                $tmp[] = $v;
            }
            return $tmp;
        }


    }

    public function remove_tag(Request $request)
    {
        $id = $request->input('id');

        if (!$id) {
            return response()->json(['code' => 0, 'msg' => '删除失败&缺少参数']);
        }

        $deleteResult = $this->tagModel::destroy($id);

        if ($deleteResult) {
            return response()->json(['code' => 1, 'msg' => '删除成功']);
        }
    }

    public function edit_tag(Request $request)
    {
        $parent_id = $request->input('parent_id');
        $enabled = $request->input('enabled');
        $name = $request->input('name');
        $tag_description = $request->input('tag_description');
        $result = $this->tagModel::where('id', $request->input('id'))->update(['tag_description' => $tag_description, 'parent_id' => $parent_id, 'enabled' => $enabled, 'name' => $name]);
        if ($result) {
            return response()->json(['code' => 1, 'msg' => '修改成功']);
        } else {
            return response()->json(['code' => 0, 'msg' => '修改失败']);
        }
    }

    public function get_tag(Request $request)
    {
        $id = $request->input('id');
        $result = $this->tagModel::find($id);
        if ($result) {
            return response()->json(['code' => 1, 'msg' => '获取成功', 'data' => $result]);
        } else {
            return response()->json(['code' => 0, 'msg' => '缺少参数']);
        }

    }
}
